package com.yanqu.road.server.cmd.activity.douluodalu;

import com.yanqu.road.entity.activity.douluodalu.config.DouLuoDaLuConfig;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_DOU_LUO_DA_LU_BUFF_BUY,desc = "Buff购买-魂师对决")
public class DouLuoDaLuBuyBattleBuffCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if ((ret = DouLuoDaLuMgr.canEntryActivity(player)) != 0) return;

        if ((ret = DouLuoDaLuMgr.activityInDuiJuePeriod()) != 0) return;

        DouLuoDaLuProto.DouLuoDaLuBuyBattleBuffReqMsg reqMsg = DouLuoDaLuProto.DouLuoDaLuBuyBattleBuffReqMsg.parseFrom(packet.getBytes());
        if(reqMsg.getBuyIndex() < 1 || reqMsg.getBuyIndex() > 3){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        DouLuoDaLuConfig config = DouLuoDaLuMgr.getConfig();
        String consume;
        if(reqMsg.getFirstBuy()){
            //首次购买
            consume = GameConfig.GAME_MONEY_INGOTS + "=" + config.FIGHT_BUFF_COST_FIRST.get((reqMsg.getBuyIndex() -1));
        }else {
            consume = GameConfig.GAME_MONEY_INGOTS + "=" + config.FIGHT_BUFF_COST.get((reqMsg.getBuyIndex() -1));
        }

        if(!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(consume))){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        player.getModule(PreReduceModule.class).preReduce(consume);

        DouLuoDaLuProto.DouLuoDaLuBuyBattleBuffCrossReqMsg.Builder crossReq = DouLuoDaLuProto.DouLuoDaLuBuyBattleBuffCrossReqMsg.newBuilder();

        crossReq.setActivityId(DouLuoDaLuMgr.getActivityInfo().getActivityId());
        crossReq.setFirstBuy(reqMsg.getFirstBuy());
        crossReq.setBuyIndex(reqMsg.getBuyIndex());
        crossReq.setConsume(consume);

        player.sendUnionActivityGroupPacket(Protocol.C_DOU_LUO_DA_LU_BUFF_BUY, crossReq, crossReq.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_DOU_LUO_DA_LU_BUFF_BUY, DouLuoDaLuProto.DouLuoDaLuBuyBattleBuffRespMsg.newBuilder().setRet(ret));
        }
    }
}
